#!/usr/bin/env python

def _(s): return s

import wx

class RemoveDatabase(wx.Dialog):
    def __init__(self, prnt, dbinfo):
        wx.Dialog.__init__(self, parent=prnt, title=_("Remove Databases"))
        self.dbinfo = dbinfo
        self.origdbs = dbinfo[:]
        topsizer = wx.BoxSizer(wx.VERTICAL)
        dblist = [db[0] for db in self.dbinfo]
        width = min(250, 250)
        height = min(250, len(dblist) * 25)
        self.lb = wx.CheckListBox(self, -1, (80, 50), (width, height), dblist)
        self.Bind(wx.EVT_CHECKLISTBOX, self.OnCheckListBox, self.lb)
        topsizer.Add(self.lb, 1, wx.EXPAND | wx.ALL, 10)
        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        CloseButton = wx.Button(self, wx.ID_CANCEL, _("&Close"))
        self.Bind(wx.EVT_BUTTON, self.OnClose, CloseButton)
        button_sizer.Add(CloseButton, 0, wx.ALL, 10)
        self.RemoveButton = wx.Button(self, wx.ID_OK, _("&Remove"))
        self.RemoveButton.Enable(False)
        self.Bind(wx.EVT_BUTTON, self.OnRemove, self.RemoveButton)
        button_sizer.Add(self.RemoveButton, 0, wx.ALL, 10)
        topsizer.Add(button_sizer, 0, wx.ALIGN_CENTER)
        self.SetSizer(topsizer)
        topsizer.SetSizeHints(self)
        self.Center()
    
    def OnClose(self, evt):
        self.Destroy()
    
    def OnCheckListBox(self, evt):
        sz = len(self.dbinfo) - 1
        selected = False
        for i in range(sz, -1, -1):
            if(self.lb.IsChecked(i)):
                selected = True
                break
        self.RemoveButton.Enable(selected)
    
    def OnRemove(self, evt):
        sz = len(self.dbinfo) - 1
        for i in range(sz, -1, -1):
            if(self.lb.IsChecked(i)):
                del self.dbinfo[i]
                self.lb.Delete(i)
        self.Destroy()
    
    def removed(self):
        return [db[1] for db in self.origdbs if db not in self.dbinfo]
    
if __name__ == "__main__":
    app = wx.PySimpleApp()
    f = RemoveDatabase(None, [("MySql", "mysql"),
                              ("Postgresql", "postgresql"),
                              ("Firebird", "firebird"),
                              ("DB2", "db2"),
                              ("Oracle", "oracle")])
    f.Show()
    app.MainLoop()
    